package utils;

import java.util.HashMap;

import se.sics.jasper.SICStus;
import se.sics.jasper.SPException;
import se.sics.jasper.SPTerm;

public class JaspertUtil {
	public SICStus engine;
	
	private JaspertUtil(SICStus engine){
		this.engine = engine;
	}
	
	public static JaspertUtil createInstance(String file) throws SPException {
		SICStus sp;

		if ((sp = SICStus.getInitializedSICStus()) == null)
			sp = new SICStus();
		if (file != null)
			sp.load(file);

		return new JaspertUtil(sp);
	}
	
	public String evaluateArithmetricExpressionVars(String expression, String vars){
		String res = null;
		HashMap<String, SPTerm> wayMap = new HashMap<String, SPTerm>();
		try {
			engine.query("eval_v( "+expression+", R, "+vars+").", wayMap);
			res = wayMap.get("R").toString();
		} catch (SPException e1) {
			e1.printStackTrace();
		}
		return res;
	}

}
